package com.edu.zut.hb.software.server.service;

import com.edu.zut.hb.software.server.pojo.Mqmsg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.Session;

@Component
public class MqProducer {
    private static final Logger logger = LoggerFactory.getLogger(MqProducer.class);

    @Resource
    private JmsMessagingTemplate jmsMessagingTemplate;
    @Autowired
    private JmsTemplate jmsTemplate;

//    @Resource
//    private Topic topic;
    @Resource
    private Queue queue;

//    public void sendQueue(String msg){
//        logger.info("发送Queue消息内容 :"+msg);
//        this.jmsMessagingTemplate.convertAndSend(this.queue,msg);
//    }
    public void sendQueue(Mqmsg mqmsg){
        this.jmsTemplate.send(queue, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                return session.createObjectMessage(mqmsg);
            }
        });
    }
    public void sendQueue1(Mqmsg mqmsg){
        this.jmsMessagingTemplate.convertAndSend(queue,mqmsg);
    }



//    public void sendTopic(String msg) {
//        System.out.println("发送Topic消息内容 :" + msg);
//        this.jmsMessagingTemplate.convertAndSend(this.topic, msg);
//    }
}